package com.quanyan.stadium.service.gameRules.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbGameRound;
import com.quanyan.stadium.entity.TbGameSchedule;
import com.quanyan.stadium.entity.TbGameScheduleAddendum;
import com.quanyan.stadium.entity.vo.VDefinedGameSchedule;
import com.quanyan.stadium.mapper.TbGameScheduleAddendumMapper;
import com.quanyan.stadium.service.gameRules.GameSystemServiceEight;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author songzj
 * @date 16/4/11-14:42
 */

@Service("GameScheduleServiceEight")
public class GameScheduleServiceEightImpl implements GameSystemServiceEight {

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private TbGameScheduleAddendumMapper tbGameScheduleAddendumMapper;

    private static final Logger logger = LoggerFactory.getLogger(GameScheduleServiceEightImpl.class);

    @Override
    public void initEightRound(List<Integer> newTeamIds, Integer curGameRoundId, Integer preGameRoundId) {
        List<VDefinedGameSchedule> currentGameSchedules = null;
        if (preGameRoundId>0){
            currentGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(preGameRoundId, null, null);
        }
        TbGameRound curGameRound = bizGameSystemDao.selectTbGameRound(curGameRoundId);
        if (null!=currentGameSchedules && currentGameSchedules.size()>0) {
            Gson gson = new Gson();
            newTeamIds = gson.fromJson(currentGameSchedules.get(0).getTeamListIndex(), new TypeToken<List<Integer>>() {
            }.getType());
            Map<Integer, Set<VDefinedGameSchedule>> mapSchedules = new HashMap<>();
            Set<VDefinedGameSchedule> setSchedule = new HashSet<>();
            for (VDefinedGameSchedule vDefinedGameSchedule : currentGameSchedules) {
                setSchedule = mapSchedules.get(vDefinedGameSchedule.getGameScheduleId());
                if (null == setSchedule || setSchedule.size() < 1) {
                    setSchedule = new HashSet<>();
                }
                setSchedule.add(vDefinedGameSchedule);
                mapSchedules.put(vDefinedGameSchedule.getGameScheduleId(), setSchedule);
            }

            for (Map.Entry<Integer, Set<VDefinedGameSchedule>> entry : mapSchedules.entrySet()) {
                List<VDefinedGameSchedule> listTemp = new ArrayList<>(entry.getValue());

                VDefinedGameSchedule vDefinedGameSchedule = listTemp.get(0);
                List<Integer> locationIndex = gson.fromJson(vDefinedGameSchedule.getTeamMatchIndex(), new TypeToken<List<Integer>>() {
                }.getType());
                if (vDefinedGameSchedule.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue()) {  //主场输
                    newTeamIds = this.eightChangeLocation(newTeamIds, locationIndex);
                }
            }
        }
        //开始生成对战
        generateEightGameSystem(newTeamIds, curGameRound);

    }

    @Override
    public void generateEightGameSystem(List<Integer> teamIds, TbGameRound curGameRound) {
        Gson gson = new Gson();
        List<Integer> first = new ArrayList<>();
        List<Integer> second = new ArrayList<>();
        int roundStatus = curGameRound.getRoundStatus();
        switch (roundStatus) {
            case 1:
                first.add(0);
                first.add(2);
                first.add(4);
                first.add(6);
                second.add(1);
                second.add(3);
                second.add(5);
                second.add(7);
                break;
            case 2:
                first.add(0);
                first.add(3);
                first.add(5);
                first.add(6);
                second.add(1);
                second.add(2);
                second.add(4);
                second.add(7);
                break;
            case 3:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                second.add(4);
                second.add(5);
                second.add(6);
                second.add(7);
                break;
            case 4:
                first.add(0);
                first.add(6);
                first.add(1);
                first.add(7);
                second.add(2);
                second.add(3);
                second.add(4);
                second.add(5);
                break;
            case 5:
                first.add(0);
                first.add(4);
                first.add(3);
                first.add(7);
                second.add(1);
                second.add(5);
                second.add(2);
                second.add(6);
                break;
            case 6:
                first.add(0);
                first.add(5);
                first.add(2);
                first.add(6);
                second.add(1);
                second.add(4);
                second.add(3);
                second.add(7);
                break;
            case 7:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                second.add(4);
                second.add(5);
                second.add(6);
                second.add(7);
                break;
            default:
                System.out.print("fuck");
        }

        TbGameSchedule schedule1 = new TbGameSchedule();
        TbGameSchedule schedule2 = new TbGameSchedule();
        TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum5 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum6 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum7 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum8 = new TbGameScheduleAddendum();

        schedule1.setGameRoundId(curGameRound.getId());
        schedule1.setTeamListIndex(gson.toJson(teamIds));
        schedule1.setTeamMatchIndex(gson.toJson(first));
        TbGameSchedule scheduleTemp11 = bizGameSystemDao.insertTbGameSchedule(schedule1);
        addendum1.setGameScheduleId(scheduleTemp11.getId());
        addendum1.setType((byte) 0);
        addendum1.setEventTeamId(teamIds.get(first.get(0)));
        addendum2.setGameScheduleId(scheduleTemp11.getId());
        addendum2.setType((byte) 0);
        addendum2.setEventTeamId(teamIds.get(first.get(1)));

        addendum3.setGameScheduleId(scheduleTemp11.getId());
        addendum3.setType((byte) 1);
        addendum3.setEventTeamId(teamIds.get(first.get(2)));
        addendum4.setGameScheduleId(scheduleTemp11.getId());
        addendum4.setType((byte) 1);
        addendum4.setEventTeamId(teamIds.get(first.get(3)));


        schedule2.setGameRoundId(curGameRound.getId());
        schedule2.setTeamListIndex(gson.toJson(teamIds));
        schedule2.setTeamMatchIndex(gson.toJson(second));
        TbGameSchedule scheduleTemp12 = bizGameSystemDao.insertTbGameSchedule(schedule2);
        addendum5.setGameScheduleId(scheduleTemp12.getId());
        addendum5.setType((byte) 0);
        addendum5.setEventTeamId(teamIds.get(second.get(0)));
        addendum6.setGameScheduleId(scheduleTemp12.getId());
        addendum6.setType((byte) 0);
        addendum6.setEventTeamId(teamIds.get(second.get(1)));

        addendum7.setGameScheduleId(scheduleTemp12.getId());
        addendum7.setType((byte) 1);
        addendum7.setEventTeamId(teamIds.get(second.get(2)));
        addendum8.setGameScheduleId(scheduleTemp12.getId());
        addendum8.setType((byte) 1);
        addendum8.setEventTeamId(teamIds.get(second.get(3)));

        tbGameScheduleAddendumMapper.insertSelective(addendum1);
        tbGameScheduleAddendumMapper.insertSelective(addendum2);
        tbGameScheduleAddendumMapper.insertSelective(addendum3);
        tbGameScheduleAddendumMapper.insertSelective(addendum4);
        tbGameScheduleAddendumMapper.insertSelective(addendum5);
        tbGameScheduleAddendumMapper.insertSelective(addendum6);
        tbGameScheduleAddendumMapper.insertSelective(addendum7);
        tbGameScheduleAddendumMapper.insertSelective(addendum8);

    }




    /**
     *  签位置交换
     *
     *
     * locationIndex里面存放上一轮的坐标值(默认从0开始)
     *
     * */
    @Override
    public List<Integer> eightChangeLocation(List<Integer> teamIds,  List<Integer> locationIndex){
        // swap the elements
        Collections.swap(teamIds, locationIndex.get(0), locationIndex.get(2));
        Collections.swap(teamIds, locationIndex.get(1), locationIndex.get(3));
        return teamIds;
    }
}
